package com.momo.mal.util;

import net.momo.mal.R;
import com.momo.mal.anime.AnimeResult;
import com.momo.mal.anime.overall.AnimeOverall;
import com.momo.mal.anime.view.AnimeView;
import com.momo.mal.database.MalDatabaseAdapter;
import com.momo.mal.entity.Anime;
import com.momo.mal.entity.Manga;
import com.momo.mal.manga.MangaResult;
import com.momo.mal.manga.view.MangaView;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;

import com.actionbarsherlock.app.SherlockDialogFragment;

public class DialogResult extends SherlockDialogFragment {
	private MalDatabaseAdapter dbAdapter;
	private String id, check;
	private Object data;
	
	public DialogResult newInstance(String id, String check, Object data) {
		DialogResult dialog = new DialogResult();
    	dialog.id = id;
    	dialog.check = check;
    	dialog.data = data;
    	
        return dialog;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
		AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
		Resources res = getResources();
		String[] items = null;
		dbAdapter = new MalDatabaseAdapter(getActivity());
		
		if(check.equalsIgnoreCase("anime")) {
			items = new String[] {
				dbAdapter.hasAnime(id) == false ? res.getString(R.string.menu_add) : res.getString(R.string.menu_edit), 
				res.getString(R.string.menu_information),
				res.getString(R.string.menu_share),
				res.getString(R.string.menu_cancel) };
			
			builder.setTitle(((Anime) data).getTitle());
		} else {
			items = new String[] {
				dbAdapter.hasManga(id) == false ? res.getString(R.string.menu_add) : res.getString(R.string.menu_edit), 
				res.getString(R.string.menu_information),
				res.getString(R.string.menu_share),
				res.getString(R.string.menu_cancel) };
			
			builder.setTitle(((Manga) data).getTitle());
		}
		
		builder.setItems(items, new DialogInterface.OnClickListener() {
			
			@Override
			public void onClick(DialogInterface dialog, int which) {
				if(which == 0) {
					if(check.equalsIgnoreCase("anime")) {
						Anime anime = new Anime();
						if(dbAdapter.hasAnime(id) == true) {
							anime = dbAdapter.getAnimeById(id);
						} else {
							anime = (Anime) data;
						}
						AnimeUtil.animeAddOrUpdatePopUp(anime, getActivity());
					} else {
						Manga manga = new Manga();
						if(dbAdapter.hasManga(id) == true) {
							manga = dbAdapter.getMangaById(id);
						} else {
							manga = (Manga) data;
						}
						MangaUtil.mangaAddOrUpdatePopUp(manga, getActivity());
					}
				}
				if(which == 1){
					if(check.equalsIgnoreCase("anime")) {
						Intent intent = new Intent(getActivity(), AnimeView.class);
						intent.putExtra("anime_id", ((Anime) data).getId());
						intent.putExtra("title", ((Anime) data).getTitle());
						startActivity(intent);
					} else {
						Intent intent = new Intent(getActivity(), MangaView.class);
						intent.putExtra("manga_id", ((Manga) data).getId());
						intent.putExtra("title", ((Manga) data).getTitle());
						startActivity(intent);
					}
				}
				if(which == 2){
					if(check.equalsIgnoreCase("anime")) {
						Anime anime = (Anime) data;
						Intent intent = new Intent(Intent.ACTION_SEND);
						intent.setType("text/plain");
						intent.putExtra(Intent.EXTRA_SUBJECT, "Recommend from your friend");
						intent.putExtra(Intent.EXTRA_TEXT, "This is recommend for you. You should try " + anime.getTitle() + ". http://myanimelist.net/anime/" + anime.getId());
						intent.putExtra(Intent.EXTRA_TITLE, "Momo MAL - " + anime.getTitle());

						startActivity(Intent.createChooser(intent, "Share Via ..."));
					} else {
						Manga manga = (Manga) data;
						Intent intent = new Intent(Intent.ACTION_SEND);
						intent.setType("text/plain");
						intent.putExtra(Intent.EXTRA_SUBJECT, "Recommend from your friend");
						intent.putExtra(Intent.EXTRA_TEXT, "This is recommend for you. You should try " + manga.getTitle() + ". http://myanimelist.net/manga/" + manga.getId());
						intent.putExtra(Intent.EXTRA_TITLE, "Momo MAL - " + manga.getTitle());

						startActivity(Intent.createChooser(intent, "Share Via ..."));
					}
				}
				if(which == 3) {
					dialog.dismiss();
				}
			}
		});
		
		return builder.create();
	}
}
